namespace DotNetNuke.Modules.Admin.ResourceInstaller
{
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Entities.Modules.Definitions;
    using DotNetNuke.Security;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.IO;
    using System.Xml;

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The PaDnnAdapter_V2 extends PaDnnAdapterBase to support V2 Modules
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// [cnurse]	04/21/2005  documented
    /// </history>
    /// -----------------------------------------------------------------------------
    public class PaDnnAdapter_V2 : PaDnnAdapterBase
    {
        public PaDnnAdapter_V2(PaInstallInfo InstallerInfo) : base(InstallerInfo)
        {
        }

        protected virtual PaFolder GetFolderAttributesFromNode(XmlElement FolderElement)
        {
            PaFolder Folder = new PaFolder();
            try
            {
                Folder.Name = FolderElement.SelectSingleNode("name").InnerText.Trim();
            }
            catch (Exception exception1)
            {
                
                Exception ex = exception1;
                throw new Exception(base.EXCEPTION_FolderName);
                
            }
            XmlElement descriptionElement = (XmlElement) FolderElement.SelectSingleNode("description");
            if (descriptionElement != null)
            {
                Folder.Description = descriptionElement.InnerText.Trim();
            }
            try
            {
                Folder.Version = FolderElement.SelectSingleNode("version").InnerText.Trim();
            }
            catch (Exception exception2)
            {
                ProjectData.SetProjectError(exception2);
                Exception ex = exception2;
                throw new Exception(base.EXCEPTION_FolderVersion);
                
            }
            Folder.FriendlyName = Folder.Name;
            Folder.FolderName = Folder.Name;
            Folder.ModuleName = Folder.Name;
            return Folder;
        }

        protected virtual PaFolder GetFolderFromNode(ref int TempModuleDefinitionID, XmlElement FolderElement)
        {
            IEnumerator refObjectHelperL0=null;
            IEnumerator refObjectHelperL1=null;
            PaFolder Folder = this.GetFolderAttributesFromNode(FolderElement);
            XmlElement resourcefileElement = (XmlElement) FolderElement.SelectSingleNode("resourcefile");
            if (resourcefileElement != null)
            {
                Folder.ResourceFile = resourcefileElement.InnerText.Trim();
                PaFile paRF = (PaFile) this.InstallerInfo.FileTable[Folder.ResourceFile.ToLower()];
                if (paRF == null)
                {
                    this.InstallerInfo.Log.AddFailure(base.EXCEPTION_MissingResource + Folder.ResourceFile);
                }
                else
                {
                    Folder.Files.Add(paRF);
                }
            }
            this.InstallerInfo.Log.AddInfo(base.FILES_Loading);
            try
            {
                refObjectHelperL0 = FolderElement.SelectNodes("files/file").GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    string name;
                    XmlElement file = (XmlElement) refObjectHelperL0.Current;
                    try
                    {
                        name = file.SelectSingleNode("name").InnerText.Trim();
                    }
                    catch (Exception exception1)
                    {
                        
                        Exception ex = exception1;
                        throw new Exception(base.EXCEPTION_FolderName);
                        
                    }
                    PaFile paf = (PaFile) this.InstallerInfo.FileTable[name.ToLower()];
                    if (paf == null)
                    {
                        this.InstallerInfo.Log.AddFailure(base.FILE_NotFound + name);
                    }
                    else
                    {
                        XmlElement pathElement = (XmlElement) file.SelectSingleNode("path");
                        if (pathElement != null)
                        {
                            string filepath = pathElement.InnerText.Trim();
                            this.InstallerInfo.Log.AddInfo(string.Format(base.FILE_Found, filepath, name));
                            paf.Path = filepath;
                        }
                        Folder.Files.Add(paf);
                    }
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
            if (!Folder.Files.Contains(this.InstallerInfo.DnnFile))
            {
                Folder.Files.Add(this.InstallerInfo.DnnFile);
            }
            this.InstallerInfo.Log.AddInfo(base.MODULES_Loading);
            try
            {
                refObjectHelperL1 = FolderElement.SelectNodes("modules/module").GetEnumerator();
                while (refObjectHelperL1.MoveNext())
                {
                    XmlElement DNNModule = (XmlElement) refObjectHelperL1.Current;
                    Folder.Modules.Add(this.GetModuleFromNode(TempModuleDefinitionID, Folder, DNNModule));
                    TempModuleDefinitionID++;
                }
            }
            finally
            {
                if (refObjectHelperL1 is IDisposable)
                {
                    (refObjectHelperL1 as IDisposable).Dispose();
                }
            }
            return Folder;
        }

        protected virtual ModuleControlInfo GetModuleControlFromNode(string Foldername, int TempModuleID, XmlElement ModuleControl)
        {
            ModuleControlInfo ModuleControlDef = new ModuleControlInfo();
            XmlElement keyElement = (XmlElement) ModuleControl.SelectSingleNode("key");
            if (keyElement != null)
            {
                ModuleControlDef.ControlKey = keyElement.InnerText.Trim();
            }
            XmlElement titleElement = (XmlElement) ModuleControl.SelectSingleNode("title");
            if (titleElement != null)
            {
                ModuleControlDef.ControlTitle = titleElement.InnerText.Trim();
            }
            try
            {
                string ControlSrc = ModuleControl.SelectSingleNode("src").InnerText.Trim();
                if (ControlSrc.ToLower().StartsWith("desktopmodules"))
                {
                    ModuleControlDef.ControlSrc = ControlSrc;
                }
                else
                {
                    ModuleControlDef.ControlSrc = Path.Combine("DesktopModules", Path.Combine(Foldername, ControlSrc));
                }
                ModuleControlDef.ControlSrc = ModuleControlDef.ControlSrc.Replace(@"\", "/");
            }
            catch (Exception exception1)
            {
                
                Exception ex = exception1;
                throw new Exception(base.EXCEPTION_Src);
                
            }
            XmlElement iconFileElement = (XmlElement) ModuleControl.SelectSingleNode("iconfile");
            if (iconFileElement != null)
            {
                ModuleControlDef.IconFile = iconFileElement.InnerText.Trim();
            }
            try
            {
                ModuleControlDef.ControlType = (SecurityAccessLevel) Conversions.ToInteger(TypeDescriptor.GetConverter(typeof(SecurityAccessLevel)).ConvertFromString(ModuleControl.SelectSingleNode("type").InnerText.Trim()));
            }
            catch (Exception exception2)
            {
                ProjectData.SetProjectError(exception2);
                Exception ex = exception2;
                throw new Exception(base.EXCEPTION_Type);
                
            }
            XmlElement orderElement = (XmlElement) ModuleControl.SelectSingleNode("vieworder");
            if (orderElement != null)
            {
                ModuleControlDef.ViewOrder = Conversions.ToInteger(orderElement.InnerText.Trim());
            }
            else
            {
                ModuleControlDef.ViewOrder = Null.NullInteger;
            }
            ModuleControlDef.ModuleDefID = TempModuleID;
            return ModuleControlDef;
        }

        protected virtual ModuleDefinitionInfo GetModuleFromNode(int TempModuleDefinitionID, PaFolder Folder, XmlElement DNNModule)
        {
            IEnumerator refObjectHelperL0=null;
            ModuleDefinitionInfo ModuleDef = new ModuleDefinitionInfo();
            XmlElement friendlyNameElement = (XmlElement) DNNModule.SelectSingleNode("friendlyname");
            if (friendlyNameElement != null)
            {
                ModuleDef.FriendlyName = friendlyNameElement.InnerText.Trim();
            }
            ModuleDef.TempModuleID = TempModuleDefinitionID;
            this.InstallerInfo.Log.AddInfo(string.Format(base.MODULES_ControlInfo, ModuleDef.FriendlyName));
            try
            {
                refObjectHelperL0 = DNNModule.SelectNodes("controls/control").GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    XmlElement ModuleControl = (XmlElement) refObjectHelperL0.Current;
                    Folder.Controls.Add(this.GetModuleControlFromNode(Folder.Name, ModuleDef.TempModuleID, ModuleControl));
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
            return ModuleDef;
        }

        protected virtual void LogValidFormat()
        {
            this.InstallerInfo.Log.AddInfo(string.Format(base.DNN_Valid, "2.0"));
        }

        public override PaFolderCollection ReadDnn()
        {
            IEnumerator refObjectHelperL1=null;
            this.InstallerInfo.Log.StartJob(base.DNN_Reading);
            ArrayList DnnErrors = this.ValidateDnn();
            if (DnnErrors.Count == 0)
            {
                IEnumerator refObjectHelperL0=null;
                this.LogValidFormat();
                PaFolderCollection Folders = new PaFolderCollection();
                XmlDocument doc = new XmlDocument();
                MemoryStream buffer = new MemoryStream(this.InstallerInfo.DnnFile.Buffer, false);
                doc.Load(buffer);
                this.InstallerInfo.Log.AddInfo(base.XML_Loaded);
                XmlNode dnnRoot = doc.DocumentElement;
                int TempModuleDefinitionID = 0;
                try
                {
                    refObjectHelperL0 = dnnRoot.SelectNodes("folders/folder").GetEnumerator();
                    while (refObjectHelperL0.MoveNext())
                    {
                        XmlElement FolderElement = (XmlElement) refObjectHelperL0.Current;
                        Folders.Add(this.GetFolderFromNode(ref TempModuleDefinitionID, FolderElement));
                    }
                }
                finally
                {
                    if (refObjectHelperL0 is IDisposable)
                    {
                        (refObjectHelperL0 as IDisposable).Dispose();
                    }
                }
                if (!this.InstallerInfo.Log.Valid)
                {
                    throw new Exception(base.EXCEPTION_LoadFailed);
                }
                this.InstallerInfo.Log.EndJob(base.DNN_Success);
                return Folders;
            }
            try
            {
                refObjectHelperL1 = DnnErrors.GetEnumerator();
                while (refObjectHelperL1.MoveNext())
                {
                    string err = Conversions.ToString(refObjectHelperL1.Current);
                    this.InstallerInfo.Log.AddFailure(err);
                }
            }
            finally
            {
                if (refObjectHelperL1 is IDisposable)
                {
                    (refObjectHelperL1 as IDisposable).Dispose();
                }
            }
            throw new Exception(base.EXCEPTION_Format);
        }

        private ArrayList ValidateDnn()
        {
            ModuleDefinitionValidator ModuleValidator = new ModuleDefinitionValidator();
            MemoryStream xmlStream = new MemoryStream(this.InstallerInfo.DnnFile.Buffer);
            ModuleValidator.Validate(xmlStream);
            return ModuleValidator.Errors;
        }
    }
}

